package com.study.jyg.rxjava2study.activity;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.appbar.AppBarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.AdapterInit;
import com.study.jyg.rxjava2study.utils.AppInfo;
import com.study.jyg.rxjava2study.utils.AppInfoUtils;
import com.study.jyg.rxjava2study.utils.AppPackageUtils;
import com.study.jyg.rxjava2study.utils.dialog.DialogUtils;
import com.study.jyg.rxjava2study.utils.hanzitopinyin.PinyinUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GetPackageSignActivity extends BasicActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appBar)
    AppBarLayout appBar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.editSearch)
    EditText editSearch;

    private List<AppInfo> datas;
    private BaseQuickAdapter<AppInfo, BaseViewHolder> adapter;
    private ClipboardManager myClipboard;
    private ClipData myClip;

    @Override
    protected int getLayout() {
        return R.layout.activity_get_package_sign;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("获取桌面APP包名");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
        myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);//复制初始化
        datas = new ArrayList<>();
        adapter = AdapterInit.initRecyclerView( recyclerView, R.layout.item_recyclerview_getpackage, new LinearLayoutManager(mContext), (helper, item) -> {
            helper.getView(R.id.ivPhoto).setBackground(item.getDrawable());
            helper.setText(R.id.tvName, item.getAppName());
            helper.setText(R.id.tvPackage, item.getPackageName());
        }, (adapter1, view, position) -> {
            DialogUtils.showDialog( getSupportFragmentManager(), R.layout.dialog_signshow, (holder, dialog) -> {
                String packageName = adapter.getItem(position).getPackageName();
                String md5 = AppInfoUtils.getSingInfo( packageName, AppInfoUtils.MD5);
                String sha1 = AppInfoUtils.getSingInfo( packageName, AppInfoUtils.SHA1);
                holder.setText( R.id.tvPackage, packageName);
                holder.setText( R.id.tvMD5, md5);
                holder.setText( R.id.tvSHA1, sha1);
                holder.getView(R.id.tvCopyPackage).setOnClickListener(v -> copy(packageName));
                holder.getView(R.id.tvCopyMD5).setOnClickListener(v -> copy(md5));
                holder.getView(R.id.tvCopySHA1).setOnClickListener(v -> copy(sha1));
            });
        });
        refreshLayout.setOnRefreshListener( refreshLayout1 -> {
            startTask();
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.autoRefresh();

        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.setNewData(search(s.toString()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 复制
     * @param text
     */
    private void copy( String text){
        myClip = ClipData.newPlainText("text", text);
        myClipboard.setPrimaryClip(myClip);
        Toast.makeText( mContext, "复制成功", Toast.LENGTH_SHORT).show();
    }

    private List<AppInfo> search(String s){
        String py = PinyinUtil.getPinYin(s);
        List<AppInfo> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            String pyi = PinyinUtil.getPinYin(datas.get(i).getAppName());
            if (pyi.equals(py) || pyi.contains(py)){
                list.add(datas.get(i));
            }
        }
        return list;
    }

    @SuppressLint("StaticFieldLeak")
    private void startTask(){
        new AsyncTask<Void, Void, List<AppInfo>>() {
            @Override
            protected List<AppInfo> doInBackground(Void... voids) {
                return AppPackageUtils.getAppInfos();
            }

            @Override
            protected void onPostExecute(List<AppInfo> appInfos) {
                super.onPostExecute(appInfos);
                datas.addAll(appInfos);
                adapter.setNewData(appInfos);
                refreshLayout.finishRefresh();
            }
        }.execute();
    }
}
